package foliotracker.GUI;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Date;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import foliotracker.controllers.BuyStockController;
import foliotracker.controllers.SellStockController;
import foliotracker.views.ISellStockView;

public class SellStockDialogue extends DialogBase implements ISellStockView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox symbolList;
	private SellStockController controller;
	private JLabel symbolError, quantityError;
	private JTextField amount;

	public SellStockDialogue(SellStockController controller){
		super("Buy Stock");
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		this.controller = controller;
		
		JLabel stockLabel = new JLabel("Symbol");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.weighty = 1.0; 
		c.weightx = 1.0;
		panel.add(stockLabel, c);
		
		symbolList = new JComboBox(controller.getSymbols().toArray());
		symbolList.setEditable(false);
		symbolList.setEditable(true);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 0;
		c.weighty = 1.0; 
		c.weightx = 1.0;
		panel.add(symbolList, c);
		
		symbolError = new JLabel();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 2;
		panel.add(symbolError, c);
	
		JLabel amountLabel = new JLabel("Amount");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 2;
		c.weighty = 1.0;
		c.weightx = 1.0;
		panel.add(amountLabel, c);
	
		
		amount = new JTextField();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 2;
		c.weightx = 1.0;
		c.weighty = 1.0; 
		panel.add(amount, c);
		
		quantityError = new JLabel();
		quantityError.setForeground(Color.RED);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 2;
		panel.add(quantityError, c);
		

		 
		 this.setSize(260, 170);
		 setPosition();
	}
	
	@Override
	protected void okButton_Clicked() {
		if(controller.validate() == true){
			controller.sellStock();
			this.setVisible(false);
		}

	}

	@Override
	protected void cancelButton_Clicked() {
		setVisible(false);
	}


	@Override
	public String getSymbol() {
		return symbolList.getSelectedItem().toString();
	}

	@Override
	public void setSymbolError(String error) {
		symbolError.setText(error);		
	}

	@Override
	public String getQuantity() {
		return amount.getText();
	}

	@Override
	public void setQuantityError(String value) {
		quantityError.setText(value);
	}


}
